package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.Service.CategoryService;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.entity.Category;
import com.ithiema.reggie.entity.Dish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//Category控制层  (菜品)
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //新增分类
    @PostMapping
    public R<String> save (@RequestBody Category category){
        categoryService.save(category);


        return R.success("新增成功");
    }


    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> lwp = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序    这是排序
        lwp.orderByAsc(Category::getSort);

        //分页查询
        categoryService.page(pageInfo,lwp);
        return R.success(pageInfo);
    }

    //删除菜品
    @DeleteMapping
    public  R<String> delete (long id){
//        categoryService.removeById(id);
        categoryService.deleteid(id);
        return R.success("删除成功");
    }

    //修改菜品
    @PutMapping
    public  R<String> update(@RequestBody Category category){
        //根据id修改
        categoryService.updateById(category);
        return R.success("修改成功");
    }



    //菜品分类查询    Category 菜品分类所在的实体类
    @GetMapping("list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lwp = new LambdaQueryWrapper<>();
        lwp.eq(category.getId()!=null,Category::getType,category.getId());
        lwp.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lwp);
        return R.success(list);
    }
}
